import { Socket } from 'socket.io';
/**
 * commandTask emit 类
 * 用于处理命令任务的事件
 */
export default class EmitTask {
  sk: Socket;
  taskName: string;

  constructor(socket: Socket, taskName: string) {
    this.sk = socket;
    this.taskName = taskName;
  }

  static SEND_MSG(socket: Socket, task: null | string, type: string, msg: string): void {
    const taskStr = task ? ` [${task}]` : '';
    const message = `[${new Date().toLocaleTimeString()}] [${type.toUpperCase()}]${taskStr}: ${msg}`;
    console.log(message);
    socket.emit('socket-task', {
      date: Date.now(),
      message,
      msg,
      task,
      type,
    });
  }
  error(msg: string): void {
    EmitTask.SEND_MSG(this.sk, this.taskName, 'error', msg || '任务发生错误');
  }

  errorEnd(msg?: string): void {
    EmitTask.SEND_MSG(this.sk, this.taskName, 'error-end', msg || '任务结束');
  }

  info(msg: string): void {
    EmitTask.SEND_MSG(this.sk, this.taskName, 'info', msg);
  }

  start(msg?: string): void {
    EmitTask.SEND_MSG(this.sk, this.taskName, 'start', msg || '任务开始');
  }

  successEnd(msg?: string): void {
    EmitTask.SEND_MSG(this.sk, this.taskName, 'success-end', msg || '任务结束');
  }
}
